
#include "thttpd.h"

void *handler_get(int sock, const char *input)
{
    int shmid;
    struct msg *p = NULL;
    key_t key = ftok("./shm.c", 'a');
    //从线程
    //对应寄存器操作
    //创建或者打开共享内存
    int shmid = shmget(key, sizeof(struct msg), IPC_CREAT | IPC_EXCL | 0666); //创建共享内存，如果之前该共享内存已存在则返回-1
    struct msg *p = NULL;
    if (shmid < 0)
    {
        if (errno == EEXIST)
            shmid = shmget(key, sizeof(struct msg), 0666); //直接打开共享内存
        else
        {
            perror("shmget err");
            return -1;
        }
    }

    //映射共享内存
    p = (struct msg *)shmat(shmid, NULL, 0);
    if (p == (struct msg *)-1)
    {
        perror("shmat err");
        return -1;
    }

    //操作共享内存
    p->flag = 0; //初始化

    u_int16_t data[32] = {0};
    while (1)
    {
        modbus_read_registers(m, 0, 4, data);
        for (int i = 0; i < 4; i++)
        {
            if (i == 0)
            {
                sprintf("光线传感器：%#x\n", data[i]);
                strcat(p->buf, "光线传感器:");
                strcat(p->buf, data[i]);
                strcat(p->buf, "\n");
            }
            else if (i == 1)
            {
                printf("加速度传感器x：%#x\n", data[i]);
                strcat(p->buf, "加速度传感器x:");
                strcat(p->buf, data[i]);
                strcat(p->buf, "\n");
            }
            else if (i == 2)
            {
                printf("加速度传感器y：%#x\n", data[i]);
                strcat(p->buf, "加速度传感器y:");
                strcat(p->buf, data[i]);
                strcat(p->buf, "\n");
            }
            else if (i == 3)
            {
                printf("加速度传感器z：%#x\n", data[i]);
                strcat(p->buf, "加速度传感器z:");
                strcat(p->buf, data[i]);
                strcat(p->buf, "\n");
            }
        }

        sleep(1);
        send(sock, p->buf, strlen(p->buf), 0);
    }
    //取消映射
    shmdt(p);
    //删除共享内存
    shmctl(shmid, IPC_RMID, NULL);
    pthread_exit(NULL);
}

void *handler_set(void *arg)
{
    //从线程
    while (1)
    {
        scanf("%d %d", &a, &b);
        if (a == 0 && b == 1)
        {
            printf("led灯打开\n");
        }
        else if (a == 0 && b == 0)
        {
            printf("led灯关闭\n");
        }
        else if (a == 1 && b == 1)
        {
            printf("蜂鸣器开\n");
        }
        else if (a == 1 && b == 0)
        {
            printf("蜂鸣器关\n");
        }
        modbus_write_bit(m, a, b);
    }
    pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
    //主线程
    pthread_t tid1;
    pthread_t tid2;
    //创建modbus实例
    m = modbus_new_tcp("192.168.50.97", 502);
    if (NULL == m)
    {
        perror("new error");
    }
    //设置从机id
    a = modbus_set_slave(m, 1);
    //与从机建立连接
    b = modbus_connect(m);

    //用来采集数据
    if (pthread_create(&tid1, NULL, handler_get, NULL) != 0)
    {
        perror("create error");
        return -1;
    }

    //用来控制硬件设备
    if (pthread_create(&tid2, NULL, handler_set, NULL) != 0)
    {
        perror("create error");
        return -1;
    }
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    //关闭实例
    modbus_close(m);
    //释放实例
    modbus_free(m);
    return 0;
}